<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="req.account" type="text" maxlength="11" placeholder="请输入手机号" :focus="isFocus"></wInput>
				<wInput v-model="req.password" type="password" maxlength="11" placeholder="请输入8位英文加数字组成的密码"></wInput>
				<!-- <wInput v-model="req.code" type="number" maxlength="4" placeholder="验证码" isShowCode ref="runCode"
					@setCode="getVerCode()"></wInput> -->
			</view>
			<wButton class="wbutton" text="登 录" @click="startLogin" :bgColor="'#ffaf32'"></wButton>

			<!-- 其他登录 -->
			<!-- <view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
			</view> -->

			<!-- 底部信息 -->
			<!-- <view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
			</view> -->
		</view>
		<u-toast ref="uToast"></u-toast>
		<view @click="link(`https://beian.miit.gov.cn`)" class="footer">粤ICP备2025415108号-1</view>
	</view>
</template>

<script>
	let _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	import {
		login,
		register,
		ResetPassword
	} from '../../api/login'
	export default {
		data() {
			return {
				logoImage: '',
				isFocus: true, // 是否聚焦
				req: {
					loginType: 'password', //登录方式账号密码登录
					account: '', //手机号
					password: '', //密码
					code: '5555'
				}
			};
		},
		components: {
			wInput,
			wButton,
		},
		onShow() {
			const token = uni.getStorageSync('token');
			if (token) {
				uni.reLaunch({
					url: '/pages/tabBar/Home/index'
				});
			}
		},
		mounted() {
			_this = this;
		},
		methods: {
			link(e) {
				window.location.href = e;
			},
			/** 
			@method 用户登录
			**/
			async startLogin(e) {
				try {
					uni.showLoading({
						title: '登录中'
					});
					const res = await login(this.req);
					if (res.code == 200) {
						this.$refs.uToast.show({
							type: "default",
							title: "默认主题",
							message: "登录成功",
							iconUrl: "https://uviewui.com/demo/toast/default.png",
							complete() {
								uni.setStorageSync('token', res.data.token);
								setTimeout(() => {
									uni.reLaunch({
										url: '/pages/tabBar/Home/index',
										fail(err) {
											console.log(err);
										}
									});
								}, 500);
							},
						});
					}
				} catch (error) {
					//TODO handle the exception
					console.log(error);
				}
			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	page {
		background: linear-gradient(83deg, #ffe8c8 4%, #fef8ec 94%);
	}
</style>
<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");

	.footer {
		text-align: center;
		color: #999;
		display: block;
	}
</style>